home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.image;
-
- import java.awt.image.ImageConsumer;
- import java.awt.image.ImageProducer;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- public abstract class InputStreamImageSource implements ImageProducer, ImageFetchable {
- PixelStore pixelstore;
- ImageConsumerQueue consumers;
- ImageDecoder decoder;
- ImageDecoder decoders;
- boolean awaitingFetch = false;
-
- public void addConsumer(ImageConsumer ic) {
- this.addConsumer(ic, false);
- }
-
- synchronized void addConsumer(ImageConsumer ic, boolean produce) {
- this.checkSecurity((Object)null, false);
-
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- if (id.isConsumer(ic)) {
- return;
- }
- }
-
- ImageConsumerQueue cq;
- for(cq = this.consumers; cq != null && cq.consumer != ic; cq = cq.next) {
- }
-
- if (cq == null) {
- cq = new ImageConsumerQueue(this, ic);
- cq.next = this.consumers;
- this.consumers = cq;
- } else {
- if (!cq.secure) {
- Object context = null;
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- context = security.getSecurityContext();
- }
-
- if (cq.securityContext == null) {
- cq.securityContext = context;
- } else if (cq.securityContext != context) {
- this.errorConsumer(cq);
- throw new SecurityException("Applets are trading image data!");
- }
- }
-
- cq.interested = true;
- }
-
- if (produce && this.decoder == null) {
- this.startProduction();
- }
-
- }
-
- private void badDecoder() {
- synchronized(this){}
-
- ImageConsumerQueue cq;
- try {
- cq = this.consumers;
- this.consumers = null;
- this.awaitingFetch = false;
- } catch (Throwable var4) {
- throw var4;
- }
-
- this.errorAllConsumers(cq);
- }
-
- abstract boolean checkSecurity(Object var1, boolean var2);
-
- synchronized int countConsumers() {
- ImageDecoder id = this.decoders;
-
- int i;
- for(i = this.countConsumers(this.consumers); id != null; id = id.next) {
- i += this.countConsumers(id.queue);
- }
-
- return i;
- }
-
- int countConsumers(ImageConsumerQueue cq) {
- int i;
- for(i = 0; cq != null; cq = cq.next) {
- ++i;
- }
-
- return i;
- }
-
- protected ImageDecoder decoderForType(InputStream is, String content_type) {
- return null;
- }
-
- public void doFetch() {
- do {
- synchronized(this) {
- if (this.consumers == null) {
- this.awaitingFetch = false;
- return;
- }
- }
- } while(this.updateFromStore());
-
- ImageDecoder imgd = this.getDecoder();
- if (imgd == null) {
- this.badDecoder();
- } else {
- this.setDecoder(imgd);
-
- try {
- imgd.produceImage();
- } catch (IOException var8) {
- ((Throwable)var8).printStackTrace();
- } catch (ImageFormatException var9) {
- ((Throwable)var9).printStackTrace();
- } finally {
- this.removeDecoder(imgd);
- this.errorAllConsumers(imgd.queue);
- }
- }
-
- }
-
- synchronized void doneDecoding(ImageDecoder mydecoder) {
- if (this.decoder == mydecoder) {
- this.decoder = null;
- if (this.consumers != null) {
- this.startProduction();
- }
- }
-
- }
-
- private void errorAllConsumers(ImageConsumerQueue cq) {
- for(; cq != null; cq = cq.next) {
- if (cq.interested) {
- this.errorConsumer(cq);
- }
- }
-
- }
-
- private void errorConsumer(ImageConsumerQueue cq) {
- cq.consumer.imageComplete(1);
- this.removeConsumer(cq.consumer);
- }
-
- synchronized void flush() {
- this.pixelstore = null;
- this.decoder = null;
- }
-
- protected abstract ImageDecoder getDecoder();
-
- protected ImageDecoder getDecoder(InputStream is) {
- if (!is.markSupported()) {
- is = new BufferedInputStream(is);
- }
-
- try {
- is.mark(6);
- int c1 = is.read();
- int c2 = is.read();
- int c3 = is.read();
- int c4 = is.read();
- is.read();
- is.read();
- is.reset();
- is.mark(-1);
- if (c1 == 71 && c2 == 73 && c3 == 70 && c4 == 56) {
- return new GifImageDecoder(this, is);
- }
-
- if (c1 == 255 && c2 == 216 && c3 == 255) {
- return new JPEGImageDecoder(this, is);
- }
-
- if (c1 == 35 && c2 == 100 && c3 == 101 && c4 == 102) {
- return new XbmImageDecoder(this, is);
- }
- } catch (IOException var6) {
- }
-
- return null;
- }
-
- public synchronized boolean isConsumer(ImageConsumer ic) {
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- if (id.isConsumer(ic)) {
- return true;
- }
- }
-
- return ImageConsumerQueue.isConsumer(this.consumers, ic);
- }
-
- void latchConsumers(ImageDecoder id) {
- ImageConsumerQueue cq;
- synchronized(this) {
- if (id != this.decoder) {
- return;
- }
-
- cq = this.consumers;
- }
-
- while(cq != null) {
- synchronized(this) {
- if (!cq.interested) {
- cq = cq.next;
- continue;
- }
- }
-
- if (!id.catchupConsumer(this, cq.consumer)) {
- synchronized(this) {
- this.doneDecoding(id);
- return;
- }
- }
-
- synchronized(this){}
-
- try {
- ImageConsumerQueue cqn = cq.next;
- if (cq.interested) {
- this.consumers = ImageConsumerQueue.removeConsumer(this.consumers, cq.consumer, true);
- cq.next = id.queue;
- id.queue = cq;
- }
-
- cq = cqn;
- } catch (Throwable var11) {
- throw var11;
- }
- }
-
- }
-
- synchronized void printQueue(ImageConsumerQueue cq, String prefix) {
- while(cq != null) {
- System.out.println(prefix + cq);
- cq = cq.next;
- }
-
- }
-
- synchronized void printQueues(String title) {
- System.out.println(title + "[ -----------");
- this.printQueue(this.consumers, " ");
-
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- System.out.println(" " + id);
- this.printQueue(id.queue, " ");
- }
-
- System.out.println("----------- ]" + title);
- }
-
- public synchronized void removeConsumer(ImageConsumer ic) {
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- id.removeConsumer(ic);
- }
-
- this.consumers = ImageConsumerQueue.removeConsumer(this.consumers, ic, false);
- if (this.consumers == null) {
- this.stopProduction();
- }
-
- }
-
- private synchronized void removeDecoder(ImageDecoder mydecoder) {
- this.doneDecoding(mydecoder);
- ImageDecoder idprev = null;
-
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- if (id == mydecoder) {
- if (idprev == null) {
- this.decoders = id.next;
- } else {
- idprev.next = id.next;
- }
- break;
- }
-
- idprev = id;
- }
-
- }
-
- public void requestTopDownLeftRightResend(ImageConsumer ic) {
- for(ImageDecoder id = this.decoders; id != null; id = id.next) {
- if (id.isConsumer(ic)) {
- id.replayConsumer(ic);
- }
- }
-
- }
-
- private void setDecoder(ImageDecoder mydecoder) {
- synchronized(this){}
-
- ImageConsumerQueue cq;
- try {
- mydecoder.next = this.decoders;
- this.decoders = mydecoder;
- this.decoder = mydecoder;
- cq = this.consumers;
- mydecoder.queue = cq;
- this.consumers = null;
- this.awaitingFetch = false;
- } catch (Throwable var5) {
- throw var5;
- }
-
- for(; cq != null; cq = cq.next) {
- if (cq.interested && !this.checkSecurity(cq.securityContext, true)) {
- this.errorConsumer(cq);
- }
- }
-
- }
-
- synchronized void setPixelStore(ImageDecoder id, PixelStore storage) {
- if (id == this.decoder) {
- this.pixelstore = storage;
- }
-
- }
-
- private synchronized void startProduction() {
- if (!this.awaitingFetch) {
- ImageFetcher.add(this);
- this.awaitingFetch = true;
- }
-
- }
-
- public void startProduction(ImageConsumer ic) {
- this.addConsumer(ic, true);
- }
-
- private synchronized void stopProduction() {
- if (this.awaitingFetch && ImageFetcher.remove(this)) {
- this.awaitingFetch = false;
- }
-
- }
-
- private boolean updateFromStore() {
- PixelStore store;
- ImageConsumerQueue cq;
- synchronized(this) {
- store = this.pixelstore;
- if (store == null) {
- return false;
- }
-
- cq = this.consumers;
- }
-
- while(cq != null) {
- boolean secure;
- synchronized(this) {
- if (!cq.interested) {
- cq = cq.next;
- continue;
- }
-
- secure = this.checkSecurity(cq.securityContext, true);
- }
-
- if (secure && !store.replay(this, cq.consumer)) {
- return false;
- }
-
- synchronized(this){}
-
- try {
- if (cq.interested) {
- this.errorConsumer(cq);
- }
-
- cq = cq.next;
- } catch (Throwable var8) {
- throw var8;
- }
- }
-
- return true;
- }
- }
-